//
//  PiShangViewController.m
//  中皮网
//
//  Created by ios程序员 on 15/10/29.
//  Copyright (c) 2015年 ios程序员. All rights reserved.
//

#import "PiShangViewController.h"



@interface PiShangViewController ()<UISearchBarDelegate,UIScrollViewDelegate,UITableViewDataSource,UITableViewDelegate,UICollectionViewDataSource,UICollectionViewDelegate,DownSheetDelegate>{
    NSArray *MenuList;
}

@property (nonatomic ,strong) BarView *barView;
@property (nonatomic ,strong) UIView *shuaixuanView;
@property (nonatomic ,strong) UITableView *sXtableView;

@end

@implementation PiShangViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}


- (void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBarHidden = YES;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationController.navigationBarHidden = YES;
    self.view.backgroundColor = [UIColor whiteColor];
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    flowLayout.itemSize = CGSizeMake(self.view.frame.size.width / 2-20, self.view.frame.size.width / 2 -20);
    flowLayout.minimumLineSpacing = 5;
    flowLayout.minimumInteritemSpacing = 5;
    flowLayout.sectionInset = UIEdgeInsetsMake(15, 15, 15, 15);
    
    self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) collectionViewLayout:flowLayout];
    
    self.collectionView.delegate = self;
    self.collectionView.dataSource =self;
    
    self.collectionView.backgroundColor = [UIColor colorWithRed:246/255.0 green:245/255.0 blue:250/255.0 alpha:1];
    
    [self.collectionView registerClass:[PiShangCollectionCell class] forCellWithReuseIdentifier:@"reuse"];
    

    [self barViewSubView];
    
    self.searchbutton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.searchbutton.frame = CGRectMake(W * 330 / 375, H * 60 / 667, W - W * 330 / 375, H * 35 / 667);
    [self.searchbutton setImage:[UIImage imageNamed:@"table.png"] forState:UIControlStateNormal];
    self.searchbutton.backgroundColor = [UIColor whiteColor];
    [self.barView addSubview:self.searchbutton];
    [self.searchbutton addTarget:self action:@selector(searchbuttonClick:) forControlEvents:UIControlEventTouchUpInside];
    
    self.tableView = [[UITableView alloc ] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStyleGrouped];
    self.tableView.rowHeight = 110;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorStyle = UITableViewCellAccessoryNone  ;
    [self.tableView registerClass:[PishangCell class] forCellReuseIdentifier:@"reuse"];
    
    [self.barView.bigScrollView addSubview:self.tableView];
    
    self.sXtableView = [[UITableView alloc] initWithFrame:CGRectMake(W*4, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStyleGrouped];
    self.sXtableView.delegate = self;
    self.sXtableView.dataSource = self;
    self.sXtableView.rowHeight = 110;
//    [self.barView.bigScrollView addSubview:self.sXtableView];
    [self.sXtableView registerClass:[PishangCell class] forCellReuseIdentifier:@"reuse"];

    
//    [self sXtableViewSubViews];
    
    [self initDemoData];
    UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(W*4, 0, self.view.frame.size.width, 60)];
    [button setTitle:@"筛选列表" forState:UIControlStateNormal];
    [button setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    button.titleLabel.font = [UIFont systemFontOfSize:13];
    [button addTarget:self action:@selector(clickMenu) forControlEvents:UIControlEventTouchUpInside];
    [self.barView.bigScrollView addSubview:button];

}
//帅选
- (void)sXtableViewSubViews
{
    self.sXtableView = [[UITableView alloc] initWithFrame:CGRectMake(W*4, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStyleGrouped];
    self.sXtableView.delegate = self;
    self.sXtableView.dataSource = self;
    [self.barView.bigScrollView addSubview:self.sXtableView];
    [self.sXtableView registerClass:[PishangCell class] forCellReuseIdentifier:@"reuse"];
    
}

- (void)searchbuttonClick:(UIButton *)button
{
    button.selected = !button.selected;
    
    if (button.selected == 1) {
        [self.searchbutton setImage:[UIImage imageNamed:@"collection.png"] forState:UIControlStateNormal];
        [self.barView.bigScrollView addSubview:self.collectionView];
    }
    else
    {
        [self.searchbutton setImage:[UIImage imageNamed:@"table.png"] forState:UIControlStateNormal];
        [self.barView.bigScrollView addSubview:self.tableView];
    }
    
}

#pragma mark --滑动scrollerView方法
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    UIButton *button1 = (UIButton *)[self.barView viewWithTag:100 + X / W ];
    [self.barView buttonClick:button1];
//    if (X >= W * 4) {
//        [self.barView.buttonScrollView setContentOffset:CGPointMake(W * 270 / 375, 0) animated:YES];
//    }else {
//        [self.barView.buttonScrollView setContentOffset:CGPointMake(0, 0) animated:YES];
//    }
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.mei.
}

//自定义barView
- (void)barViewSubView
{
    self.barView = [[BarView alloc] initWithFrame:CGRectMake(0, 0 , W , 608*H/667 )];
    self.barView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.barView];
    self.barView.search.delegate = self;
    self.barView.bigScrollView.delegate = self;
}

#pragma tableview 的两个协议方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView == self.tableView) {
        return 10;
    }else if (tableView == self.sXtableView){
        return 0;
    }else{
        return 0;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    PishangCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuse" ];
    if (tableView == self.tableView) {
        //    cell.backgroundColor = [UIColor purpleColor];
//        cell.firstImage.backgroundColor = [UIColor redColor];
//        cell.firstLabel.backgroundColor = [UIColor blueColor];
//        cell.upLabel.backgroundColor = [UIColor greenColor];
//        cell.dowenLabel.backgroundColor = [UIColor blackColor];
//        cell.rightLabel.backgroundColor = [UIColor purpleColor];
        cell.backgroundColor = [UIColor colorWithRed:246/255.0 green:245/255.0 blue:250/255.0 alpha:1];

    }else if(tableView == self.sXtableView ){
//        cell.firstImage.backgroundColor = [UIColor redColor];
//        cell.firstLabel.backgroundColor = [UIColor blueColor];
//        cell.upLabel.backgroundColor = [UIColor greenColor];
//        cell.dowenLabel.backgroundColor = [UIColor blackColor];
//        cell.rightLabel.backgroundColor = [UIColor purpleColor];

    }
    
    return cell;
    
}
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForFooterInSection:(NSInteger)section{
    
    return 120;
}


- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 150;
}

//跳转方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    PSTableViewController *pst = [[PSTableViewController alloc] init];
    [self.navigationController pushViewController:pst animated:NO];
    self.navigationController.navigationBarHidden = NO;
}

//--------------------------------

#pragma  collectionView 的两个协议方法
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 20;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    PiShangCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor colorWithRed:236/255.0 green:245/255.0 blue:250/255.0 alpha:1];
//    cell.imageView.backgroundColor = [UIColor redColor];
//    cell.jianjieLabel.backgroundColor = [UIColor yellowColor];
//    cell.addLabel.backgroundColor = [UIColor greenColor];
//    cell.baoyouLabel.backgroundColor = [UIColor purpleColor];
//    cell.moneyLabel.backgroundColor = [UIColor blackColor];
//    cell.yishouLabel.backgroundColor = [UIColor blueColor];
    
    return cell;
    
}
//跳转方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    PSCollectionViewController *psc = [[PSCollectionViewController alloc] init];
    [self.navigationController pushViewController:psc animated:NO];
    self.navigationController.navigationBarHidden = NO;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    return CGSizeMake(self.view.frame.size.width, 0);
}

-(void)initDemoData{
    DownSheetModel *Model_1 = [[DownSheetModel alloc]init];
    Model_1.icon = @"";
    Model_1.icon_on = @"";
    Model_1.title = @"风格";
    DownSheetModel *Model_2 = [[DownSheetModel alloc]init];
    Model_2.icon = @"";
    Model_2.icon_on = @"";
    Model_2.title = @"选购热点";
    DownSheetModel *Model_3 = [[DownSheetModel alloc]init];
    Model_3.icon = @"";
    Model_3.icon_on = @"";
    Model_3.title = @"相关分类";
    DownSheetModel *Model_4 = [[DownSheetModel alloc]init];
    Model_4.icon = @"";
    Model_4.icon_on = @"";
    Model_4.title = @"发货地";
    DownSheetModel *Model_5 = [[DownSheetModel alloc]init];
    Model_5.icon = @"";
    Model_5.icon_on = @"";
    Model_5.title = @"";
    DownSheetModel *Model_6 = [[DownSheetModel alloc]init];
    Model_6.icon = @"";
    Model_6.icon_on = @"";
    Model_6.title = @"";
    MenuList = [[NSArray alloc]init];
    MenuList = @[Model_1,Model_2,Model_3,Model_4,Model_5,Model_6];
}

-(void)clickMenu{
    DownSheet *sheet = [[DownSheet alloc]initWithlist:MenuList height:0];
    sheet.delegate = self;
    [sheet showInView:nil];
}

-(void)didSelectIndex:(NSInteger)index{
//    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:[NSString stringWithFormat:@"您当前点击的是第%ld个按钮",(long)index] delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
//    [alert show];
    
    
    
}



/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
